package zl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.bean.Category;
import zl.mapper.CategoryMapper;
import zl.mapper.NovelMapper;
import zl.service.CategoryService;

import java.sql.SQLException;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private NovelMapper novelMapper;

    @Override
    public int addCategory(Category category) {
        Category u = categoryMapper.selectByname(category.getName());
        if(u==null){
            return categoryMapper.addCategory(category);
        }else {
            return 3;
        }
    }

    @Override
    public int deleteCategory(Integer id) throws SQLException {
        long count1 = novelMapper.selectChildren(id);
        long count2 = categoryMapper.selectChildren(id);
        if(count1==0 && count2==0) {
            return categoryMapper.deleteCategory(id);
        }else {
            return 3;
        }
    }

    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    @Override
    public List<Category> selectAllCategory() {
        return novelMapper.selectAllCategory(null);
    }

    @Override
    public Category selectById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public PageInfo<Category> selectByPage(int currentPage, String keyword) throws SQLException {
        PageHelper.startPage(currentPage, 5);
        List<Category> list =novelMapper.selectAllCategory(keyword);
        PageInfo<Category> pageInfo = new PageInfo<>(list);
        System.out.println("总记录数：" + pageInfo.getTotal());
        System.out.println("当前页：" + pageInfo.getPageNum());
        System.out.println("总页数：" + pageInfo.getPages());
        System.out.println("上一页：" + pageInfo.getPrePage());
        System.out.println("下一页：" + pageInfo.getNextPage());
        System.out.println("是否是首页：" + pageInfo.isIsFirstPage());
        System.out.println("是否是尾页：" + pageInfo.isIsLastPage());
        return pageInfo;
    }
}
